<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Nhan
 * Date: 8/13/12
 * Time: 9:58 AM
 * To change this template use File | Settings | File Templates.
 */

//add demo - metabox plugin part
include 'demo.php';

//add custom javascript
require_once(TEMPLATEPATH.'/include/theme-js.php');

//widgets ready
require_once(TEMPLATEPATH.'/include/theme-widget.php');

// Register widgetized areas
function theme_widgets_init() {
    register_sidebar(array (
        'name'=>'Sidebar-Wide - Top',
        'id'=>'widget-1',
        'before_widget' => '<div id="%1$s" class="widget %2$s"><div class="widget-wrap">',
        'after_widget' => '</div></div>',
        'before_title' => '<h3 class="widget-title"><span>',
        'after_title' => '</span></h3>',
    ));

    register_sidebar(array (
        'name'=>'Sidebar-Wide - Bottom Left',
        'id'=>'widget-2',
        'before_widget' => '<div id="%1$s" class="widget %2$s"><div class="widget-wrap">',
        'after_widget' => '</div></div>',
        'before_title' => '<h3 class="widget-title"><span>',
        'after_title' => '</span></h3>',
    ));
    register_sidebar(array (
        'name'=>'Sidebar-Wide - Bottom Right',
        'id'=>'widget-3',
        'before_widget' => '<div id="%1$s" class="widget %2$s"><div class="widget-wrap">',
        'after_widget' => '</div></div>',
        'before_title' => '<h3 class="widget-title"><span>',
        'after_title' => '</span></h3>',
    ));
    register_sidebar(array (
        'name'=>'Sidebar-Bottom-Last',
        'id'=>'widget-4',
        'before_widget' => '<div id="%1$s" class="widget %2$s"><div class="widget-wrap">',
        'after_widget' => '</div></div>',
        'before_title' => '<h3 class="widget-title"><span>',
        'after_title' => '</span></h3>',
    ));
}

add_action( 'init', 'theme_widgets_init' );

// Add theme support for Featured Images
if ( function_exists( 'add_theme_support' ) ) {
    add_theme_support( 'post-thumbnails' );
}

// Add support for WP 3.0 Menu Management
if (function_exists('add_theme_support')) {
    add_theme_support('menus');
}

if (function_exists('register_nav_menus')) {
    function register_my_menus() {
        register_nav_menus(array(
                'main-nav' => __( 'Menu Chính' ),
                'location-cat' => __( 'Phân loại: Địa điểm' ),
                'price-cat' => __( 'Phân loại: Giá' ),
                'square-cat' => __( 'Phân loại: Diện Tích' )
            )
        );
    }
    add_action( 'init', 'register_my_menus' );
}


/*
 * Register some post-type (Custom post type UI output)
 */
add_action('init','nhan_register_post_type');

function nhan_register_post_type() {
    register_post_type('tintuc',
        array(
            'label' => 'Tin Tức',
            'description' => '','public' => true,
            'show_ui' => true,'show_in_menu' => true,
            'capability_type' => 'post','hierarchical' => true,
            'rewrite' => array(
                'slug' =>  'tintuc',
                'with_front' =>  false,
            ),
            'query_var' => true,
            'has_archive' => true,
            'supports' => array('title','thumbnail','editor','excerpt','comment'),
            'labels' => array (
                'name' => 'Tin Tức',
                'singular_name' => 'Tin Tức',
                'menu_name' => 'Tin Tức',
                'add_new' => 'Thêm Tin Tức',
                'add_new_item' => 'Thêm Tin',
                'edit' => 'Sửa nội dung tin tức',
                'edit_item' => 'Sửa nội dung',
                'new_item' => 'Thêm Mới',
                'view' => 'Xem',
                'view_item' => 'Xem',
                'search_items' => 'Tìm Nhà Trọ',
                'not_found' => 'Không tìm thấy',
                'not_found_in_trash' => 'Không tìm thấy',
                'parent' => 'Parent',
            ),) );
}
/*
 * Auto generate readmore link after excerpt
 */
function excerpt_read_more_link($output) {
    global $post;
    return $output . '<a href="'. get_permalink($post->ID) . '"> Chi tiết...</a>';
}
add_filter('the_excerpt', 'excerpt_read_more_link');

/*
 * Function from plugin : multi category search
 */
function multicategories_search()
{
    if (function_exists('dropdown_manager_paintdropdown'))
    {
        $cats = array();

        $number_of_categories = dropdown_get_dropdowns();
        for($i=0;$i<($number_of_categories);$i++)
        {

            if ($_POST['dropdown_plugin_dropdowns_'.$i] != "")
            {
                $cats[] = $_POST['dropdown_plugin_dropdowns_'.$i];
            }
        }
        if (count($cats) == 0)
        {

            $cats[] = 99999999;
        }
        query_posts(array('category__and' => $cats));
    }
}
/*
 * Remove postype support
 */
	add_action('init', 'my_custom_init');

	function my_custom_init() {
        //remove_post_type_support( 'post', 'editor' );
}
/*
 * Get-Set post views - count
 */
function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, 0);
        return "0 View";
    }
    return $count;
}
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, 0);
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}
/*
 * Get recent comment
 */
function getRecentComments() {
    $pre_HTML ="";
    $post_HTML ="";
    global $wpdb;
    $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_author_email, comment_date_gmt, comment_approved, comment_type,comment_author_url, SUBSTRING(comment_content,1,100) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 5";

    $comments = $wpdb->get_results($sql);
    $output = $pre_HTML;
    $output .= "\n<ul>";
    $count = 0;
    foreach ($comments as $comment) {
        $output .= "\n<li><a href=\"" . get_permalink($comment->ID)."#comment-" . $comment->comment_ID . "\" title=\"on ".$comment->post_title . "\">" . strip_tags($comment->comment_author) .": " . "<span class='commentExceprt'>".strip_tags($comment->com_excerpt)."</span></a></li>";
        $count++;
    }
    $output .= "\n</ul>";
    $output .= $post_HTML;
    if($count == 0) {
        echo "Chưa có bình luận nào!";
    }
    else echo $output;
}
/*
 * Zoom - pretty photo Jquery Plugin
 */
add_action('wp_enqueue_scripts', 'add_my_jscripts');

if ( !function_exists( 'add_my_jscripts' ) ) {

    function add_my_jscripts() {

        wp_register_script('prettyPhoto', get_bloginfo('template_url')."/js/jquery.prettyPhoto.js", array('jquery'), '');

        wp_register_script('enable-prettyPhoto', get_bloginfo('template_url')."/js/enablePrettyPhoto.js", array('jquery','prettyPhoto'), '');

        //load this js everywhere on frontend
        wp_enqueue_script('enable-prettyPhoto');
    }
}
//Add my css files to front-end
add_action('wp_print_styles', 'add_my_cssfiles');

if ( !function_exists( 'add_my_cssfiles' ) ) {

    function add_my_cssfiles() {

        if ( wp_script_is ('prettyPhoto', 'queue' ) ) {

            wp_register_style( 'prettyPhotoCSS', get_bloginfo('template_url')."/css/prettyPhoto.css" , '', '1.0', 'screen' );

            wp_enqueue_style( 'prettyPhotoCSS' );
        }
    }
}
/*
 * Add some custom admin style
 */
function custom_admin_style() {
    echo '<style type="text/css">
            .rwmb-input input[type=text] {
                width:638px;
            }
         </style>';
}
add_action('admin_head', 'custom_admin_style');